package com.poisonh.unifiedauth.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.okhttputils.model.HttpParams;
import com.poisonh.unifiedauth.R;
import com.poisonh.unifiedauth.dao.UserinfoDaoImpl;
import com.poisonh.unifiedauth.model.UserInfoModel;
import com.poisonh.unifiedauth.utils.ConstantUitls;
import com.poisonh.unifiedauth.utils.MyHttpsUtils;
import com.poisonh.unifiedauth.utils.NetConnectUtil;
import com.poisonh.unifiedauth.utils.ToastUtils;
import com.poisonh.unifiedauth.widget.CircleImageView;
import com.zhy.autolayout.AutoRelativeLayout;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 个人信息
 * Created by PoisonH on 2016/6/28.
 */
public class SettingShouMyinfoActivity extends BaseActivity implements View.OnClickListener
{
    private AutoRelativeLayout mARLLHead;
    private AutoRelativeLayout mARLLName;
    private AutoRelativeLayout mARLLPhone;
    private AutoRelativeLayout mARLLEmail;
    private AutoRelativeLayout mARLLBadyInfo;
    private AutoRelativeLayout mARLLUserLevel;
    private AutoRelativeLayout mARLLScore;

    private CircleImageView mSettingShowMyinfoHead;
    private TextView mSettingShowMyinfoUserName;
    private TextView mSettingShowMyinfoUserPhone;
    private TextView mSettingShowMyinfoUserEmail;
    private TextView mSettingShowMyinfoUserBadyInfo;
    private TextView mSettingShowMyinfoUserLevel;
    private TextView mSettingShowMyinfoScore;

    private String mStrReallyName;
    private String mStrCard;
    private String mStrHeadUrl;
    private String mStrName;


    @Override
    protected void loadContentView(Bundle savedInstanceState)
    {
        setContentView(R.layout.activity_settingshowmyinfo);
        initTitleView();
        initView();
    }

    @Override
    protected void initView()
    {
        mRightBtn.setVisibility(View.GONE);
        setTitleTxt("个人信息");
        mARLLHead = (AutoRelativeLayout) findViewById(R.id.arl_setting_myinfo_a);
        mARLLName = (AutoRelativeLayout) findViewById(R.id.arl_setting_myinfo_b);
        mARLLPhone = (AutoRelativeLayout) findViewById(R.id.arl_setting_myinfo_c);
        mARLLEmail = (AutoRelativeLayout) findViewById(R.id.arl_setting_myinfo_d);
        mARLLBadyInfo = (AutoRelativeLayout) findViewById(R.id.arl_setting_myinfo_e);
        mARLLUserLevel = (AutoRelativeLayout) findViewById(R.id.arl_setting_myinfo_f);
        mARLLScore = (AutoRelativeLayout) findViewById(R.id.arl_setting_myinfo_g);

        mSettingShowMyinfoHead = (CircleImageView) findViewById(R.id.civ_settingshowmyinfo_head);
        mSettingShowMyinfoUserName = (TextView) findViewById(R.id.tv_settingshowmyinfo_username);
        mSettingShowMyinfoUserPhone = (TextView) findViewById(R.id.tv_settingshowmyinfo_phone);
        mSettingShowMyinfoUserEmail = (TextView) findViewById(R.id.tv_settingshowmyinfo_email);
        mSettingShowMyinfoUserBadyInfo = (TextView) findViewById(R.id.tv_settingshowmyinfo_badyinfo);
        mSettingShowMyinfoUserLevel = (TextView) findViewById(R.id.tv_settingshowmyinfo_level);
        mSettingShowMyinfoScore = (TextView) findViewById(R.id.tv_settingshowmyinfo_score);

        mARLLHead.setOnClickListener(this);
        mARLLName.setOnClickListener(this);
        mARLLPhone.setOnClickListener(this);
        mARLLEmail.setOnClickListener(this);
        mARLLBadyInfo.setOnClickListener(this);
        mARLLUserLevel.setOnClickListener(this);
        mARLLScore.setOnClickListener(this);
        mLeftBtn.setOnClickListener(this);
        //   SVProgressHUD.showWithStatus(this, true);
    }


    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.arl_setting_myinfo_a:
                jumpActivity(SettingHeadImgActivity.class);
                break;
            case R.id.arl_setting_myinfo_b:
                //   ConstantUitls.mStrName = mStrName;
                jumpActivity(SettingMyNameActivity.class);
                break;
            case R.id.arl_setting_myinfo_c:
                jumpActivity(ShowOldPhoneActivity.class);
                break;
            case R.id.arl_setting_myinfo_d:
                super.initUserData();
                if (mStrUserEmail == null)
                {
                    jumpActivity(InputEmailActivity.class);
                } else
                {
                    jumpActivity(ChangeEmailActivity.class);
                }
                break;
            case R.id.arl_setting_myinfo_e:
                if (mStrReallyName != null || mStrCard != null)
                {
                    Intent mIntent = new Intent(SettingShouMyinfoActivity.this, SettingPerfectActivity.class);
                    Bundle mBundle = new Bundle();
                    mBundle.putString("REALLYNAME", mStrReallyName);
                    mBundle.putString("CARD", mStrCard);
                    mBundle.putString("HEADURL", mStrHeadUrl);
                    mIntent.putExtras(mBundle);
                    startActivity(mIntent);
                } else
                {
                    jumpActivity(SettingBindInfoActivity.class);
                }
                break;
            case R.id.arl_setting_myinfo_f:
                jumpActivity(SettingUpLevelActivity.class);
                break;
            case R.id.arl_setting_myinfo_g:
                jumpActivity(SettingScoreActivity.class);
                break;
            case R.id.ib_left_btn:
                finish();
                break;
        }
    }

    /**
     * 页面跳转
     *
     * @param activity
     */
    protected void jumpActivity(Class activity)
    {
        startActivity(new Intent(SettingShouMyinfoActivity.this, activity));
    }

    private void loadMyInfo()
    {
        HttpParams params = new HttpParams();
        params.put("id", mStrUserId);
        params.put("token", mStrUserToken);
        MyHttpsUtils.post(ConstantUitls.REQUEDT_URL + ConstantUitls.FINDUSERINFO, params, new MyHttpsUtils.ResultCallback()
        {
            @Override
            public void onSuccess(String response)
            {
                Log.d("LoginActivity", "response::::" + response);
                Message msg = Message.obtain();
                msg.obj = response;
                mHandler.sendMessage(msg);
                // SVProgressHUD.showWithStatus(SettingShouMyinfoActivity.this, false);
            }

            @Override
            public void onFailure(String failReason)
            {
                ToastUtils.showToast(SettingShouMyinfoActivity.this, failReason);
                // SVProgressHUD.showWithStatus(SettingShouMyinfoActivity.this, false);
            }
        });
    }

    Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            super.handleMessage(msg);
            try
            {
                //根据id查询用户
                UserinfoDaoImpl mUserinfoDaoImpl = new UserinfoDaoImpl(SettingShouMyinfoActivity.this);
                UserInfoModel model = mUserinfoDaoImpl.queryReturnOne(mStrUserId);

                JSONObject object = new JSONObject((String) msg.obj);
                JSONObject data = object.getJSONObject("results");
                if (!data.getString("realName").equals("null"))
                {
                    mStrReallyName = data.getString("realName");
                    model.setmStrReallyName(mStrReallyName);
                }
                if (!data.getString("name").equals("null"))
                {
                    mStrName = data.getString("name");
                    mSettingShowMyinfoUserName.setText(data.getString("name"));
                    model.setmStrUserName(mStrName);
                }
                if (!data.getString("phone").equals("null"))
                {
                    String mStrPhone = data.getString("phone");
                    mSettingShowMyinfoUserPhone.setText(data.getString("phone"));
                    model.setmStrPhone(mStrPhone);
                }
                model.setmStrPhone(data.getString("phone"));
                if (!data.getString("email").equals("null"))
                {
                    String mStrEmail = data.getString("email");
                    mSettingShowMyinfoUserEmail.setText(mStrEmail);
                    model.setmStrUserEmail(mStrEmail);
                }
                if (!data.getString("card").equals("null"))
                {
                    mStrCard = data.getString("card");
                    mSettingShowMyinfoUserBadyInfo.setText(mStrCard);
                    model.setmStrCard(mStrCard);
                }
                if (!data.getString("education").equals("null"))
                {
                    mSettingShowMyinfoScore.setText(data.getString("education"));
                    model.setmStrEducation(data.getString("education"));
                }
                if (!data.getString("headImgUrl").equals("null"))
                {
                    mStrHeadUrl = ConstantUitls.REQUEDT_IMG_URL + data.getString("headImgUrl");
                    Glide.with(getApplication()).load(mStrHeadUrl).into(mSettingShowMyinfoHead);
                    model.setmStrHeadUrl(mStrHeadUrl);
                }
                mSettingShowMyinfoUserLevel.setText(data.getString("score"));
                model.setmStringScore(data.getString("score"));
                mUserinfoDaoImpl.commit();

            } catch (JSONException e)
            {
                e.printStackTrace();
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    };


    @Override
    protected void onResume()
    {
        super.onResume();
        //根据网络状态加载数据
        if (NetConnectUtil.isNetConnected(this))
        {
            loadMyInfo();
        } else
        {
            try
            {
                //根据id查询用户
                UserinfoDaoImpl mUserinfoDaoImpl = new UserinfoDaoImpl(SettingShouMyinfoActivity.this);
                UserInfoModel model = mUserinfoDaoImpl.queryReturnOne(mStrUserId);


                if (model.getmStrReallyName() != null)
                {
                    mStrReallyName = model.getmStrReallyName();
                }
                if (model.getmStrUserName() != null)
                {
                    mStrName = model.getmStrUserName();
                    mSettingShowMyinfoUserName.setText(mStrName);
                }
                if (model.getmStrPhone() != null)
                {
                    mSettingShowMyinfoUserPhone.setText(model.getmStrPhone());
                }
                if (model.getmStrUserEmail() != null)
                {
                    //  mStrEmail = model.getmStrUserEmail();
                    mSettingShowMyinfoUserEmail.setText(model.getmStrUserEmail());
                }
                if (model.getmStrCard() != null)
                {
                    mStrCard = model.getmStrCard();
                    mSettingShowMyinfoUserBadyInfo.setText(mStrCard);
                }
                if (model.getmStrEducation() != null)
                {
                    mSettingShowMyinfoScore.setText(model.getmStrEducation());
                }
                if (model.getmStrHeadUrl() != null)
                {
                    // mStrHeadUrl = ConstantUitls.mStrHeadUrl = model.getmStrHeadUrl();
                    Glide.with(getApplication()).load(mStrHeadUrl).into(mSettingShowMyinfoHead);
                }
                if (model.getmStringScore() != null)
                {
                    mSettingShowMyinfoUserLevel.setText(model.getmStringScore());
                }
                mUserinfoDaoImpl.commit();
                //   SVProgressHUD.showWithStatus(SettingShouMyinfoActivity.this, false);
            } catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }

}
